package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private EditText userId=null;
    private EditText userPassword=null;
    private Button confirmButton=null;
    private Button cancelButton=null;
    private RadioGroup radioGroup=null;
    private String sex=null;
    private CheckBox cb1,cb2,cb3;
    private String likes=null;


    //onCreate相当于主界面的构造函数,程序运行时自动调用
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        userId=findViewById(R.id.user_id);
        userPassword=findViewById(R.id.user_password);
        confirmButton=findViewById(R.id.confirm_button);
        cancelButton=findViewById(R.id.cancel_button);
        radioGroup=findViewById(R.id.sex_radiogroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if(i==R.id.man)
                {
                    sex="男";
                }
                else if(i==R.id.woman)
                {
                    sex="女";
                }
            }
        });

        cb1=findViewById(R.id.cb_game);
        cb2=findViewById(R.id.cb_sport);
        cb3=findViewById(R.id.cb_read);

        //写一个监听器
        CompoundButton.OnCheckedChangeListener cbListener=new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b)
                {
                    if(likes==null)
                    {
                        likes=compoundButton.getText().toString()+" ";
                    }
                    else
                    {
                        likes+=compoundButton.getText().toString()+" ";
                    }
                }
            }
        };
        cb1.setOnCheckedChangeListener(cbListener);
        cb2.setOnCheckedChangeListener(cbListener);
        cb3.setOnCheckedChangeListener(cbListener);
    }

    public void confirm_click(View view) {
        String msg="用户名:"+userId.getText()+"  "+"密码:"+userPassword.getText()+"  "+"性别"+sex+" "+"喜欢"+likes;
        Toast.makeText(MainActivity.this,msg,Toast.LENGTH_LONG).show();
    }

//    public void confirm_click(View view)
//    {
//        String msg="用户名:"+userId.getText()+"  "+"密码:"+userPassword.getText();
//        Toast.makeText(MainActivity.this,msg,Toast.LENGTH_LONG).show();
//    }
}